// pages/classification/classification.js
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shangpinlist:[],
        showname:false,
        showdel:false,
        addname:false,
        addshowname:"",
        allupdata:{},
        possin:[
            '家具生活',
            '数码电器',
            '美妆护肤',
            '母婴'
        ]
    },
    onClose(){
        this.setData({
            showname:false,
            showdel:false,
            addname:false,
            addshowname:"",
            allupdata:{}
        })
    },
    addfenlei(){
        if (this.data.addshowname == "") {
            wx.showToast({
                title: '还未输入,请输入！',
                icon: 'none',
                duration: 2000
            })
        }else{
            let reqdata={
                currentTarget:{dataset:{select:this.data.addshowname}}
            }
            this.possinselect(reqdata)
        }
    },
    updatacread(items){
        this.setData({
            allupdata:this.data.shangpinlist[items.currentTarget.dataset.select],
            addshowname:this.data.shangpinlist[items.currentTarget.dataset.select].categoryName
        })
        this.setData({
            showname:true
        })
    },
    opendelcread(items){
        this.setData({
            allupdata:this.data.shangpinlist[items.currentTarget.dataset.select],
        })
        this.setData({
            showdel:true
        })
    },
    async delcread(items){
        let reqdata = {
            id:this.data.allupdata.id
        }
        let resdata = await basesup.delstoreClass(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
            })
            this.getshangpin()
        }else{
            wx.showToast({
                title: '删除失败,请重试！',
                icon: 'error',
                duration: 2000
            })
        }
    },
    async updatafenlei(){
        let reqdata = {
            "id": this.data.allupdata.id,
            "categoryName": this.data.addshowname,
        }
        let resdata = await basesup.updatastoreClass(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 2000
            })
            this.getshangpin()
        }else{
            wx.showToast({
                title: '添加失败,请重试！',
                icon: 'error',
                duration: 2000
            })
        }
    },
    naveclasssort() {
        wx.navigateTo({
            url: '../../pages/classsort/classsort',
        })
    },
    alertinput(){
        this.setData({
            addname:true
        })
    },
    async possinselect(items){
        let reqdata = {
            categoryName:items.currentTarget.dataset.select
        }
        let resdata = await basesup.addstoreClass(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 2000
            })
            this.getshangpin()
        }else{
            wx.showToast({
                title: '添加失败,请重试！',
                icon: 'error',
                duration: 2000
            })
        }
    },
    async getshangpin(){
        let resdata = await basesup.storeClass()
        this.setData({
            shangpinlist:resdata.data.Result.records
        })
        this.onClose()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getshangpin()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getshangpin()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})